#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

homedir="/etc/cloudreve"

get_config() {
	config_get_bool enabled $1 enabled 0
	config_get lstport $1 port 5212
}

start_service() {
	config_load cloudreve
	config_foreach get_config cloudreve
	[ $enabled != 1 ] && return 1
	mkdir -p "$homedir"
	if [ -f "$homedir/cloudreve.ini" ]; then
		sed -i "s/Listen = :[0-9]*/Listen = :$lstport/g" "$homedir/cloudreve.ini"
	else
		cat >"$homedir/cloudreve.ini"<<EOF
[System]
Debug = false
Mode = master
Listen = :${lstport}
EOF
	fi
	procd_open_instance
	procd_set_param command /usr/bin/cloudreve -c $homedir/cloudreve.ini
	procd_set_param stderr 1
	procd_set_param respawn
	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "cloudreve"
}

stop_service() {
    killall -q cloudreve  >&2
    echo "CloudReve Server has stopped."  >&2
}
